#include <sdk.h>
#include <globals.h>
#include "encodingdetector.h"

#include <wx/filename.h>

#include "TextFileSearcher.h"
#include "TextFileSearcherText.h"
#include "TextFileSearcherRegEx.h"


TextFileSearcher* TextFileSearcher::BuildTextFileSearcher( const wxString& searchText,
    bool matchCase,
    bool matchWordBegin,
    bool matchWord,
    bool regEx ) {
  TextFileSearcher* pFileSearcher = NULL;
  if( regEx == true ) {
    pFileSearcher = new TextFileSearcherRegEx( searchText, matchCase, matchWordBegin, matchWord );
  } else {
    pFileSearcher = new TextFileSearcherText( searchText, matchCase, matchWordBegin, matchWord );
  }
  wxString errorMessage( wxEmptyString );
  if( pFileSearcher && !pFileSearcher->IsOk( &errorMessage ) ) {
    delete pFileSearcher;
    pFileSearcher = 0;
  }
  return pFileSearcher;
}


TextFileSearcher::eFileSearcherReturn TextFileSearcher::FindInFile( const wxString& filePath, wxArrayString &foundLines ) {
  eFileSearcherReturn success = idStringNotFound;
  wxString line;
  if( wxFileName::GetSize( filePath ) != 0 ) {
    if( !wxFileName::FileExists( filePath ) ) {
      return idFileNotFound;
    }
    EncodingDetector enc( filePath, false );
    if( !enc.IsOK() ) {
      return idFileNotFound;
    }
    wxFontEncoding fe =  enc.GetFontEncoding();
    if( fe == wxFONTENCODING_UTF7 ) {
      wxMBConvUTF7 conv;
      if( !m_TextFile.Open( filePath, conv ) ) {
        return idFileOpenError;
      }
    } else if( fe == wxFONTENCODING_UTF8 ) {
      wxMBConvUTF8 conv;
      if( !m_TextFile.Open( filePath, conv ) ) {
        return idFileOpenError;
      }
    } else if( fe == wxFONTENCODING_UTF16BE ) {
      wxMBConvUTF16BE conv;
      if( !m_TextFile.Open( filePath, conv ) ) {
        return idFileOpenError;
      }
    } else if( fe == wxFONTENCODING_UTF16LE ) {
      wxMBConvUTF16LE conv;
      if( !m_TextFile.Open( filePath, conv ) ) {
        return idFileOpenError;
      }
    } else if( fe == wxFONTENCODING_UTF32BE ) {
      wxMBConvUTF32BE conv;
      if( !m_TextFile.Open( filePath, conv ) ) {
        return idFileOpenError;
      }
    } else if( fe == wxFONTENCODING_UTF32LE ) {
      wxMBConvUTF32LE conv;
      if( !m_TextFile.Open( filePath, conv ) ) {
        return idFileOpenError;
      }
    } else {
      wxCSConv conv( fe );
      if( !m_TextFile.Open( filePath, conv ) ) {
        return idFileOpenError;
      }
    }
    for( size_t i = 0; i < m_TextFile.GetLineCount(); ++i ) {
      line = m_TextFile.GetLine( i );
      if( MatchLine( line ) ) {
        success = idStringFound;
        line.Replace( _T( "\t" ), _T( " " ) );
        line.Replace( _T( "\r" ), _T( " " ) );
        line.Replace( _T( "\n" ), _T( " " ) );
        line.Trim( false );
        line.Trim( true );
        foundLines.Add( F( _T( "%lu" ), static_cast<unsigned long>( i + 1 ) ) );
        foundLines.Add( line );
      }
    }
    m_TextFile.Close();
  }
  return success;
}


bool TextFileSearcher::IsOk( wxString* ( pErrorMessage ) ) {
  return true;
}
